﻿using Core.Dtos;
using Core.Domain;
using Core.Messages;
using Core.Queries;
using Core.ViewModels;
using Library.Cache;
using Library.Helpers;
using Library.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Core.Mappers;
using Core.Aspects;
using Core.IRepository.Admin;
using Core.IServices;
using Core.IRepository;

namespace Services.Admin
{
    public class ReadLocalizationService : BaseService, IReadLocalizationService
    {
        private const string CACHE_LOCALE_STRINGS = "LocaleStrings";

        private readonly IAdminRepository<int, LocaleString> repository;
        private readonly IAdminUnitOfWork unitOfWork;

        public ReadLocalizationService(IAdminUnitOfWork unitOfWork, IAdminRepository<int, LocaleString> repository)
        {
            this.repository = repository;        
            this.unitOfWork = unitOfWork;        
        }

        [Cache(Name = CACHE_LOCALE_STRINGS, NoExpiration = true)]
        public IEnumerable<LocaleStringDto> GetAll()
        {
            return repository.FindAll().Map<LocaleString, LocaleStringDto>();
        }

        public void InitStaticCache()
        {
            GetAll();
        }

        [DeleteCache(Name = CACHE_LOCALE_STRINGS)]
        public Response ResetStaticCache()
        {
            return Run(() =>
            {
                InitStaticCache();
            });
        }

        public IEnumerable<LocaleStringDto> GetAllForClient()
        {
            var all = GetAll();
            IEnumerable<LocaleStringDto> list = new List<LocaleStringDto>();
            list = list.Concat(all.FindByKey("Messages.Success"));
            return list;
        }

        public string Get(string cultureCode, string name)
        {
            return GetAll().Get(cultureCode, name);
        }
    }
}
